package com.hyk.commonLib.common.dialogView;


import android.content.Context;

import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.databinding.Bindable;

import com.hyk.commonLib.common.dialogView.base.CenterPopupViewExt;
import com.hyk.commonLib.common.entity.ImageSource;
import com.hyk.commonLib.common.listener.OnClickResultListener;
import com.hyk.commonLib.common.utils.ResUtils;
import com.hyk.commonLib.common.utils.eventHandler.ViewEventHandler;
import com.hyk.commonLib.common.utils.viewModel.BaseObservableModel;
import com.hyk.commonLib.databinding.CommonLibDialogViewConfirmBinding;

import org.apache.commons.lang3.StringUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

public class ConfirmDialog extends CenterPopupViewExt<CommonLibDialogViewConfirmBinding> {
    public ConfirmDialog(@NonNull Context context) {
        super(context);
        ConfirmDialogModel model = new ConfirmDialogModel();
        ConfirmDialogEventHandler eventHandler = new ConfirmDialogEventHandler(this, model);

        dataBinding.setModel(model);
        dataBinding.setHandler(eventHandler);
    }

    @Getter(onMethod_ = {@Bindable})
    @Setter(onMethod_ = {@BaseObservableModel.PropertiesChanged(value = true)})
    @Accessors(chain = true)
    public static class ConfirmDialogModel extends BaseObservableModel {
        @Setter(onMethod_ = {@PropertiesChanged(value = true, names = {"showTitle"})})
        private CharSequence title;
        @Setter(onMethod_ = {@PropertiesChanged(value = true, names = {"showContent"})})
        private CharSequence content;
        @ButtonType
        @Setter(onMethod_ = {@PropertiesChanged(value = true, names = {"showSingleBtn", "showDoubleBtn"})})
        private int buttonType = ButtonType.SINGLE_CONFIRM;
        @Setter(onMethod_ = {@PropertiesChanged(value = true, names = {"hasTopImg"})})
        private ImageSource topImage;

        // region ButtonType.SINGLE 时使用
        private CharSequence singleBtnText = "确定";
        @ColorInt
        private int singleBtnTextColor = ResUtils.Color.textColorAccent;
        @Setter
        @Getter(AccessLevel.PROTECTED)
        private OnClickResultListener onSingleBtnClickListener;
        // endregion

        // region ButtonType.TWO_EQUALLY 时使用
        private CharSequence leftBtnText = "取消";
        private CharSequence rightBtnText = "确定";
        @ColorInt
        private int leftBtnTextColor = ResUtils.Color.textColorSecondary;
        @ColorInt
        private int rightBtnTextColor = ResUtils.Color.textColorAccent;
        @Setter
        @Getter(AccessLevel.PROTECTED)
        private OnClickResultListener onLeftBtnClickListener;
        @Setter
        @Getter(AccessLevel.PROTECTED)
        private OnClickResultListener onRightBtnClickListener;
        // endregion

        @Bindable
        public boolean isShowTitle() {
            return StringUtils.isNotEmpty(title);
        }

        @Bindable
        public boolean isShowContent() {
            return StringUtils.isNotEmpty(content);
        }

        @Bindable
        public boolean isHasTopImg() {
            return topImage != null && topImage.notEmpty();
        }

        @Bindable
        public boolean isShowSingleConfirm() {
            return buttonType == ButtonType.SINGLE_CONFIRM;
        }

        @Bindable
        public boolean isShowConfirmAndCancel() {
            return buttonType == ButtonType.CONFIRM_AND_CANCEL_EQUALLY;
        }
    }

    public static class ConfirmDialogEventHandler extends ViewEventHandler<ConfirmDialog, ConfirmDialogModel> {
        public ConfirmDialogEventHandler(@NonNull ConfirmDialog view, ConfirmDialogModel model) {
            super(view, model);
        }

        public void onSingleBtnClicked() {
            OnClickResultListener onSingleClick = getModel().getOnSingleBtnClickListener();
            if (onSingleClick == null) {
                getContainer().dismiss();
                return;
            }
            boolean blocked = onSingleClick.onClick();
            if (blocked) return;
            getContainer().dismiss();
        }

        public void onLeftBtnClicked() {
            OnClickResultListener onLeftClick = getModel().getOnLeftBtnClickListener();
            if (onLeftClick == null) {
                getContainer().dismiss();
                return;
            }
            boolean blocked = onLeftClick.onClick();
            if (blocked) return;
            getContainer().dismiss();
        }

        public void onRightBtnClicked() {
            OnClickResultListener onRightClick = getModel().getOnRightBtnClickListener();
            if (onRightClick == null) {
                getContainer().dismiss();
                return;
            }
            boolean blocked = onRightClick.onClick();
            if (blocked) return;
            getContainer().dismiss();
        }
    }


    @IntDef({ButtonType.SINGLE_CONFIRM, ButtonType.CONFIRM_AND_CANCEL_EQUALLY})
    @Retention(RetentionPolicy.SOURCE)
    public @interface ButtonType {
        int SINGLE_CONFIRM = 0;
        int CONFIRM_AND_CANCEL_EQUALLY = 1;
    }
}
